package com.xudm.imp.utils;

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.entity.mime.FileBody;
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
import org.apache.hc.client5.http.entity.mime.StringBody;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;

@Component
public class HttpClientUtil {
    private final CloseableHttpClient httpClient;

    @Autowired
    public HttpClientUtil(CloseableHttpClient httpClient) {
        this.httpClient = httpClient;
    }

    public String sendGetRequest(String url, Map<String, String> headers) throws IOException, ParseException {
        HttpGet request = new HttpGet(url);
        if (headers != null) {
            headers.forEach(request::addHeader);
        }
        try (CloseableHttpResponse response = httpClient.execute(request)) {
            return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
        }
    }

    public String sendPostRequest(String url, String jsonPayload, Map<String, String> headers) throws IOException, ParseException {
        HttpPost request = new HttpPost(url);
        request.setEntity(new StringEntity(jsonPayload, ContentType.APPLICATION_JSON));
        if (headers != null) {
            headers.forEach(request::addHeader);
        }
        try (CloseableHttpResponse response = httpClient.execute(request)) {
            return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
        }
    }

    public String sendPostRequestWithForm(String url, Map<String, String> formParams, Map<String, String> headers) throws IOException, ParseException {
        HttpPost request = new HttpPost(url);
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        formParams.forEach((key, value) -> builder.addPart(key, new StringBody(value, ContentType.TEXT_PLAIN)));
        request.setEntity(builder.build());
        if (headers != null) {
            headers.forEach(request::addHeader);
        }
        try (CloseableHttpResponse response = httpClient.execute(request)) {
            return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
        }
    }

    public String sendPostRequestWithFile(String url, Map<String, String> formParams, File file, Map<String, String> headers) throws IOException, ParseException {
        HttpPost request = new HttpPost(url);
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        formParams.forEach((key, value) -> builder.addPart(key, new StringBody(value, ContentType.TEXT_PLAIN)));
        builder.addPart("file", new FileBody(file));
        request.setEntity(builder.build());
        if (headers != null) {
            headers.forEach(request::addHeader);
        }
        try (CloseableHttpResponse response = httpClient.execute(request)) {
            return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
        }
    }

}
